using UnityEngine;
using UnityEngine.UI;

public class UIIndicator : MonoBehaviour
{
	public RectTransform rectTransform;

	public Text nameText;

	public Image arrowImage;

	private Camera _mainCamera;

	private Hole _target;

	private void Awake()
	{
		_mainCamera = Camera.main;
	}

	public void SetTarget(Hole target)
	{
		_target = target;
		nameText.text = target.Pseudo;
		nameText.color = target.CurrentSkinParameters.mainColor;
		arrowImage.color = target.CurrentSkinParameters.mainColor;
	}

	public void UpdatePosition()
	{
		if (!_target)
		{
			return;
		}
		if (_target.IsDead)
		{
			base.gameObject.SetActive(value: false);
			return;
		}
		Vector3 position = _target.transform.position;
		float z = position.z;
		Vector3 position2 = _mainCamera.transform.position;
		if (z < position2.z - 15f)
		{
			Vector3 position3 = _mainCamera.transform.position;
			position.z = position3.z - 15f;
		}
		Vector3 v = _mainCamera.WorldToViewportPoint(position);
		if (v.x >= 0f && v.x <= 1f && v.y >= 0f && v.y <= 1f)
		{
			base.gameObject.SetActive(value: false);
			return;
		}
		base.gameObject.SetActive(value: true);
		rectTransform.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, MathUtils.Vector2ToAngle(v) + 180f));
		v.x = Mathf.Clamp(v.x, 0.05f, 0.95f);
		v.y = Mathf.Clamp(v.y, 0.1f, 0.95f);
		rectTransform.anchorMin = new Vector2(v.x, v.y);
		rectTransform.anchorMax = new Vector2(v.x, v.y);
	}
}
